<?php


class Category extends Api\Abstracte\Controller
{

    public $data = [];

    public function __construct()
    {
        parent::__construct();
        $this->load->model('common_model');
        $this->load->model('mysql_model');
        $this->common_model=$this->common_model;
        $this->mysql_model = $this->mysql_model;
        $this->data = [
            "parentId"=>1352,
            "name"=>"食用油",
            "typeNumber"=>"trade",
            "id"=>1353,
        ];

    }

    public function add(){
        //$data = $this->validform(str_enhtml($this->input->post(NULL,TRUE)));
        //exit(var_dump($data));
        $data = $this->data;
        switch ($data['typeNumber']) {
            case 'trade':
                $this->trade_add($data);break;
            case 'supplytype':
                $success = '新增供应商类别:';break;
            case 'customertype':
                $success = '新增客户类别:';break;
            case 'raccttype':
                $success = '新增收入类别:';break;
            case 'paccttype':
                $success = '新增支出类别:';break;
            case 'PayMethod':
                $success = '新增结算方式:';break;
            default:
                str_alert(-1,'参数错误');
        }
        $sql  = $this->mysql_model->insert('category',elements(array('name','typeNumber'),$data));
        if ($sql) {
            //$this->common_model->logs($success.$data['name']);
            die('{"status":200,"msg":"success","data":{"coId":0,"detail":true,"id":'.$sql.',"level":1,"name":"'.$data['name'].'","parentId":0,"remark":"","sortIndex":2,"status":0,"typeNumber":"'.$data['typeNumber'].'","uuid":""}}');
        }
        str_alert(-1,'添加失败');
    }

    //修改
    public function update(){
        $data = $this->validform(str_enhtml($this->input->post(NULL,TRUE)));
        switch ($data['typeNumber']) {
            case 'trade':
                $this->trade_update($data);break;
            case 'supplytype':
                $success = '修改供应商类别:';break;
            case 'customertype':
                $success = '修改客户类别:';break;
            case 'raccttype':
                $success = '修改收入类别:';break;
            case 'paccttype':
                $success = '修改支出类别:';break;
            case 'PayMethod':
                $success = '修改结算方式:';break;
            default:
                str_alert(-1,'参数错误');
        }
        $sql = $this->mysql_model->update('category',elements(array('name','typeNumber'),$data),array('id'=>$data['id']));
        if ($sql) {
            die('{"status":200,"msg":"success","data":{"coId":0,"detail":true,"id":'.$data['id'].',"level":1,"name"
		:"'.$data['name'].'","parentId":0,"remark":"","sortIndex":2,"status":0,"typeNumber":"'.$data['typeNumber'].'","uuid":""
		}}');
        }
        str_alert(-1,'修改失败');
    }

    //分类删除
    public  function delete() {
        $id   = intval($this->input->post('id',TRUE));
        $type = str_enhtml($this->input->get_post('typeNumber',TRUE));
        switch ($type) {
            case 'trade':
                $success = '删除商品类别:';break;
            case 'supplytype':
                $success = '删除供应商类别:';break;
            case 'customertype':
                $success = '删除客户类别:';break;
            case 'raccttype':
                $success = '删除收入类别:';break;
            case 'paccttype':
                $success = '删除支出类别:';break;
            case 'PayMethod':
                $success = '删除结算方式:';break;
            default:
                str_alert(-1,'参数错误');
        }
        $data = $this->mysql_model->get_rows('category',array('id'=>$id));
        if (count($data)>0) {
            $this->mysql_model->get_count('goods',array('isDelete'=>0,'categoryId'=>$id))>0 && str_alert(-1,'辅助资料已经被使用');
            $this->mysql_model->get_count('contact',array('isDelete'=>0,'cCategory'=>$id))>0 && str_alert(-1,'辅助资料已经被使用');
            $this->mysql_model->get_count('category','(isDelete=0) and find_in_set('.$id.',path)')>1 && str_alert(-1,'不能删除，请先删除子分类！');
            $sql = $this->mysql_model->update('category',array('isDelete'=>1),array('id'=>$id));
            if ($sql) {
                str_alert(200,'success',array('msg'=>'删除成功','id'=>'['.$id.']'));
            }
        }
        str_alert(-1,'删除失败');
    }

    //商品分类添加
    private function trade_add($data){
        if ($data['parentId']==0) {
            $data['level'] = 1;
            $newid = $this->mysql_model->insert('category',elements(array('name','typeNumber','level','parentId'),$data));
            $sql   = $this->mysql_model->update('category',array('path'=>$newid),array('id'=>$newid));
        } else {
            $cate = $this->mysql_model->get_rows('category',array('id'=>$data['parentId']));
            count($cate)<1 && str_alert(-1,'参数错误');
            $data['level'] = $cate['level'] + 1;
            $newid = $this->mysql_model->insert('category',elements(array('name','typeNumber','level','parentId'),$data));
            $sql   = $this->mysql_model->update('category',array('path'=>$cate['path'].','.$newid),array('id'=>$newid));
        }
        if ($sql) {
            die('{"status":200,"msg":"success","data":{"coId":0,"detail":true,"id":'.$newid.',"level":'.$data['level'].',"name":"'.$data['name'].'","parentId":'.$data['parentId'].',"remark":"","sortIndex":1,"status":0,"typeNumber":"'.$data['typeNumber'].'","uuid":""}}');
        }
        str_alert(-1,'添加失败');
    }

    //公共验证
    private function validform($data) {
        $data['typeNumber'] = str_enhtml($this->input->get_post('typeNumber',TRUE));  //结算方式是GET
        $data['id']         = isset($data['id']) ? intval($data['id']) :0;
        $data['parentId']   = isset($data['parentId']) ? intval($data['parentId']):0;
        strlen($data['name']) < 1 && str_alert(-1,'类别名称不能为空');
        strlen($data['typeNumber']) < 1 && str_alert(-1,'参数错误');
        $where['isDelete']   = 0;
        $where['name']       = $data['name'];
        $where['typeNumber'] = $data['typeNumber'];
        $where['id !=']      = $data['id']>0 ? $data['id'] :0;
        $this->mysql_model->get_count('category',$where) > 0 && str_alert(-1,'类别名称重复');
        return $data;
    }

    public function index(){
        exit('hello');
    }
}